{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "client_id = \"QaBXdSIyVVhDQbcYgViXnk2z\"\n",
    "client_secret = \"ta3jtL4LcNnnibGeAsLOSijLVMscwotX\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.515552819394bb2e1c5407dff303c17d.315360000.1918869632.282335-22868514', 'expires_in': 2592000, 'session_key': '9mzdX+njg2z2jJPYDEq6NQW1BKOUgON2bFOrBhBzhgQNxVYJQur1RhXjtforJGZyuGDqfLAcvRG24nnRBSFqMdoahIsxOA==', 'access_token': '24.d3b7223b291eb70af1c0bbc7ccfb5435.2592000.1606101632.282335-22868514', 'scope': 'public brain_all_scope vis-faceverify_faceverify_h5-face-liveness vis-faceverify_FACE_V3 vis-faceverify_idl_face_merge vis-faceverify_FACE_EFFECT vis-faceverify_face_feature_sdk wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component', 'session_secret': 'ab5276c66e35f462cd9881a487d931d3'}\n"
     ]
    }
   ],
   "source": [
    " # encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=QaBXdSIyVVhDQbcYgViXnk2z&client_secret=ta3jtL4LcNnnibGeAsLOSijLVMscwotX&'\n",
    "response = requests.get(host)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2.人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 9489001001354, 'timestamp': 1603508739, 'cached': 0, 'result': {'score': 94.37832642, 'face_list': [{'face_token': '91cf0a5aa45b0371989e56760b30548c'}, {'face_token': '93d2501e1cb1685c26107d0b19cc854a'}]}}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/match\"\n",
    "\n",
    "params = \"[{\\\"image\\\": \\\"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3430692674,459091344&fm=26&gp=0.jpg\\\", \\\"image_type\\\": \\\"URL\\\", \\\"face_type\\\": \\\"CERT\\\", \\\"quality_control\\\": \\\"LOW\\\"}, {\\\"image\\\": \\\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603015508446&di=eee4e2c852d804bc3b80a719df3df9ef&imgtype=0&src=http%3A%2F%2Fimg2-cloud.itouchtv.cn%2Ftvtouchtv%2Fimage%2F20170914%2F1505363583630378.jpg\\\", \\\"image_type\\\": \\\"URL\\\", \\\"face_type\\\": \\\"LIVE\\\", \\\"quality_control\\\": \\\"LOW\\\"}]\"\n",
    "access_token = '24.6d684a22feb8384590448aa0f4edcb8e.2592000.1605596154.282335-22838595'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3.创建用户组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 4584998435751,\n",
       " 'timestamp': 1603509424,\n",
       " 'cached': 0,\n",
       " 'result': None}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/faceset/group/add\"\n",
    "\n",
    "params = \"{\\\"group_id\\\":\\\"group2\\\"}\"\n",
    "access_token = '24.ca5cf3338c04a585b126a3f39894a286.2592000.1606101255.282335-22868514' # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "### 制成表格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>refresh_token</th>\n",
       "      <th>expires_in</th>\n",
       "      <th>session_key</th>\n",
       "      <th>access_token</th>\n",
       "      <th>scope</th>\n",
       "      <th>session_secret</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>25.515552819394bb2e1c5407dff303c17d.315360000....</td>\n",
       "      <td>2592000</td>\n",
       "      <td>9mzdX+njg2z2jJPYDEq6NQW1BKOUgON2bFOrBhBzhgQNxV...</td>\n",
       "      <td>24.d3b7223b291eb70af1c0bbc7ccfb5435.2592000.16...</td>\n",
       "      <td>public brain_all_scope vis-faceverify_faceveri...</td>\n",
       "      <td>ab5276c66e35f462cd9881a487d931d3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                       refresh_token  expires_in  \\\n",
       "0  25.515552819394bb2e1c5407dff303c17d.315360000....     2592000   \n",
       "\n",
       "                                         session_key  \\\n",
       "0  9mzdX+njg2z2jJPYDEq6NQW1BKOUgON2bFOrBhBzhgQNxV...   \n",
       "\n",
       "                                        access_token  \\\n",
       "0  24.d3b7223b291eb70af1c0bbc7ccfb5435.2592000.16...   \n",
       "\n",
       "                                               scope  \\\n",
       "0  public brain_all_scope vis-faceverify_faceveri...   \n",
       "\n",
       "                     session_secret  \n",
       "0  ab5276c66e35f462cd9881a487d931d3  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "form = pd.json_normalize(response.json())\n",
    "form"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 4.人脸注册"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 1019420110125,\n",
       " 'timestamp': 1603510092,\n",
       " 'cached': 0,\n",
       " 'result': {'face_token': '91cf0a5aa45b0371989e56760b30548c',\n",
       "  'location': {'left': 186.93,\n",
       "   'top': 99.21,\n",
       "   'width': 121,\n",
       "   'height': 118,\n",
       "   'rotation': 1}}}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add\"\n",
    "\n",
    "params = \"{\\\"image\\\":\\\"91cf0a5aa45b0371989e56760b30548c\\\",\\\"image_type\\\":\\\"FACE_TOKEN\\\",\\\"group_id\\\":\\\"group2\\\",\\\"user_id\\\":\\\"user1\\\",\\\"user_info\\\":\\\"abc\\\",\\\"quality_control\\\":\\\"LOW\\\",\\\"liveness_control\\\":\\\"NORMAL\\\"}\"\n",
    "access_token = '24.a239c9a0fb0d502b96dc8c4cd3c25b23.2592000.1606101980.282335-22861642' # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 6.删除用户组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 223100,\n",
       " 'error_msg': 'group not exist',\n",
       " 'log_id': 589158484259,\n",
       " 'timestamp': 1603510205,\n",
       " 'cached': 0,\n",
       " 'result': None}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/faceset/group/delete\"\n",
    "\n",
    "params = \"{\\\"group_id\\\":\\\"group1\\\"}\"\n",
    "access_token = '24.a239c9a0fb0d502b96dc8c4cd3c25b23.2592000.1606101980.282335-22861642' # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
